package com.ruoyi.project.user.order.service.impl;

import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.mapper.UserMapper;
import com.ruoyi.project.user.company.mapper.CompanyMapper;
import com.ruoyi.project.user.company.service.CompanyService;
import com.ruoyi.project.user.company.service.impl.CompanyServiceImpl;
import com.ruoyi.project.user.host.domain.Host;
import com.ruoyi.project.user.host.mapper.HostMapper;
import com.ruoyi.project.user.order.domain.Order;
import com.ruoyi.project.user.order.mapper.OrderMapper;
import com.ruoyi.project.user.order.service.OrderService;
import com.ruoyi.project.user.people.mapper.TCoupleMapper;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class OrderServiceImpl implements OrderService {
    
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private HostMapper hostMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private TCoupleMapper tCoupleMapper;
    
    @Override
    public List<Order> showOrder(Order order) {
        return orderMapper.selectByOrder(order);
    }

    @Override
    public int addOrder(Order order) {
//        Date date=new Date();
        User user=new User();
        Host host=new Host();
        //******userId
        user.setUserName(order.getOrderCustomerName());
        Long userId=userMapper.selectUserIdByName(user);
        order.setOrderUserId(userId);
        //*****hostId
        host.setHostName(order.getOrderHostname());
        Long hostId=hostMapper.selectHostIdByName(host);
        //*****主持人电话
        Host host1=hostMapper.selectByHostid(hostId);
        order.setOrderHostphone(host1.getHostPhone());
        order.setOrderHostId(hostId);
        //*****下单角色
        
        Long index=companyMapper.selectCompanyByName(order.getOrderCompany());//
        Long index2=hostMapper.selectOrderByHostname(order.getOrderCompany());
        if (order.getOrderCompany().equals("新人")){
            order.setOrderRole(3);
        }else {
            if (index==1){//婚庆公司
                order.setOrderRole(1);
            }
            if (index2==1){//主持人
                order.setOrderRole(4);
            }
        }
       //****时间
        if (order.getOrderCeremonytime()==0){
            order.setOrderAfternoontime(1);
            order.setOrderMorningtime(1);
        }else if (order.getOrderCeremonytime()==1){
            order.setOrderMorningtime(1);
        }else {
            order.setOrderAfternoontime(1);
        }
        
        
//        System.out.println("*******************"+date+"*******************");
//        order.setOrderCreatetime(date);
        return orderMapper.insertOrder(order);
    }

    @Override
    public Long checkHostname(String orderHostname) {
      //  System.out.println("****************"+orderHostname);
        return hostMapper.selectOrderByHostname(orderHostname);
    }

    @Override
    public Long checkCompany(String orderCompany) {
        //*****orderCompany公司名
        
        Long index=companyMapper.selectCompanyByName(orderCompany);
        Long index2=hostMapper.selectOrderByHostname(orderCompany);
        if (orderCompany.equals("新人")){
            return Long.valueOf(1);
        }else {
            if (index==1){
                return Long.valueOf(1);
            }
            if (index2==1){//主持人
                return Long.valueOf(1);
            }
            
        }
        return Long.valueOf(0);
    }

    @Override
    public Long checkName(String orderCustomerName) {

       Long index= tCoupleMapper.selectIdByName(orderCustomerName);//查询下单人姓名是否存在
    //    System.out.println("-****************************"+index+"******************************");
        return index;
    }

    @Override
    public Long checkPhone(String orderPhone,String orderCustomerName) {
        System.out.println("******"+orderPhone+"******"+orderCustomerName);
        return userMapper.selectNameByPhone(orderPhone,orderCustomerName);
    }

    @Override
    public Long checkCeremonytime(Integer orderCeremonytime,String orderHostname,Date orderCeremonydatestart) {
        
        //orderCeremonytime（0全天,1上午,2下午）
        //state(0空闲，1有单)
        //根据主持人姓名和时间查询是否有单  排除已删除订单
        Long day=orderMapper.selectOrderByStart(orderHostname,orderCeremonydatestart);//数据库中是否有这天的数据
    // 存在一条数据 当天有单   存在主持人全天有单或上午有单，或下午有单
        if (day==1) {
            //上午是否有单
            Integer state1=orderMapper.selectStateById1(orderHostname,orderCeremonydatestart);//order_morningtime
            //下午是否有单
            Integer state2=orderMapper.selectStateById2(orderHostname,orderCeremonydatestart);//order_afternoontime
//            System.out.println("******state1="+state1+"**********");
//            System.out.println("******state2="+state2+"**********");
        // 如果用户选全天
            if (orderCeremonytime == 0 ) {
                //上午和下午都没单
                if ((state1 == 0 )&& (state2 == 0 ) ){
                    //可以
                    return Long.valueOf(1);
                }
                //返回错误
                return Long.valueOf(0);
        //如果用户选上午
            } else if (orderCeremonytime == 1) {
                //上午有单
                if (state1 == 1) {
                    //返回错误
                    return Long.valueOf(0);
                }
                //可以
                return Long.valueOf(1);
        //用户选择下午
            } else if (orderCeremonytime == 2) {
                //下午有单
                if (state2 == 1) {
                    //返回错误
                    return Long.valueOf(0);
                }
                //可以
                return Long.valueOf(1);
            }
        //其他情况都是错    
            return Long.valueOf(0);
           
        }else if (day>1){//一天有两条数据不能再添加
            return Long.valueOf(0);
        }
        return Long.valueOf(1);//当天每单
    }


    @Override
    public Long checkCeremonytime2(Integer orderCeremonytime,String orderHostname,Date orderCeremonydatestart,Long  orderId) {

        //orderCeremonytime（0全天,1上午,2下午）
        //state(0空闲，1有单)
        Order order=orderMapper.selectByOrderId(orderId);//回显订单
        Long day=orderMapper.selectOrderByStart(orderHostname,orderCeremonydatestart);//数据库中是否有这天的数据
        Date dateStart=order.getOrderCeremonydatestart();//回显订单时间
        int orderMorningtime=order.getOrderMorningtime();//回显数据
        int orderAfternoontime=order.getOrderAfternoontime();//回显数据
//        System.out.println("*******************"+dateStart);
//        System.out.println("*********************"+orderCeremonydatestart);
        if(dateStart.equals(orderCeremonydatestart)){//要修改的时间和回显的时间一致
            if(orderMorningtime==0&&orderAfternoontime==0){//上午和下午全部空闲
                return Long.valueOf(1);
            }else {
             //   System.out.println("**********order="+order.toString()+"*****************");
                //将回显原始数据订单状态修改为空闲
                order.setOrderMorningtime(0);
                order.setOrderAfternoontime(0);
                orderMapper.updateOrder(order);
           //     System.out.println("**********order="+order.toString()+"*****************");
                
                if (day==1) {//当天有单
                    Integer state1=orderMapper.selectStateById1(orderHostname,orderCeremonydatestart);//该主持人在该天上午的状态
                    Integer state2=orderMapper.selectStateById2(orderHostname,orderCeremonydatestart);//该主持人在该天下午的状态
                   // System.out.println("******state1="+state1+"**********");
                  //  System.out.println("******state2="+state2+"**********");
                    if (orderCeremonytime == 0 ) {//选全天

                        if ((state1 == 0 )&& (state2 == 0 ) ){//上午和下午都没单
                            return Long.valueOf(1);
                            
                        }
                        return Long.valueOf(0);//返回错误
                    } else if (orderCeremonytime == 1) {
                        if (state1 == 1) {
                            return Long.valueOf(0);
                        }
                        return Long.valueOf(1);
                    } else if (orderCeremonytime == 2) {
                        if (state2 == 1) {
                            return Long.valueOf(0);
                        }
                        return Long.valueOf(1);
                    }
                    return Long.valueOf(0);
                }else if (day>1){//一天有两条数据不能再添加
                    return Long.valueOf(0);
                }
                return Long.valueOf(1);//当天每单
            }
        }
        else {
            if (day==1) {//当天有单
                Integer state1=orderMapper.selectStateById1(orderHostname,orderCeremonydatestart);//该主持人在该天上午的状态
                Integer state2=orderMapper.selectStateById2(orderHostname,orderCeremonydatestart);//该主持人在该天下午的状态
//            System.out.println("******state1="+state1+"**********");
//            System.out.println("******state2="+state2+"**********");
                if (orderCeremonytime == 0 ) {//选全天

                    if ((state1 == 0 )&& (state2 == 0 ) ){//上午和下午都没单
                        return Long.valueOf(1);
                    }
                    return Long.valueOf(0);//返回错误
                } else if (orderCeremonytime == 1) {
                    if (state1 == 1) {
                        return Long.valueOf(0);
                    }
                    return Long.valueOf(1);
                } else if (orderCeremonytime == 2) {
                    if (state2 == 1) {
                        return Long.valueOf(0);
                    }
                    return Long.valueOf(1);
                }
                return Long.valueOf(0);
            }else if (day>1){//一天有两条数据就说明上午和下午分别有任务，不能再添加
                return Long.valueOf(0);
            }
            return Long.valueOf(1);
        }
        //要修改的时间和回显的时间不一致
        
        //1.判断当天是否有单
        //有单进行下一步
        //每单直接通过
       // return Long.valueOf(1);//当天没单
    }
    
    
    @Override
    public Order showOrderById(Long orderId) {
        return orderMapper.selectByOrderId(orderId);
    }

    @Override
    public int updateOrder(Order order) {
        Long time=(long)(int)order.getOrderCeremonytime();//
        Long state=(long)(int)order.getOrderState();//订单状态
        if (time==0){ //用户选全天 将状态进行修改  忙
            order.setOrderMorningtime(1);
            order.setOrderAfternoontime(1);
        }else if (time==1){
            order.setOrderMorningtime(1);
        }else if (time==2){
            order.setOrderAfternoontime(1);
        }
        
        if (state==2){//用户删除订单，将状态修改为空闲
            order.setOrderCeremonytime(3);
            order.setOrderMorningtime(0);
            order.setOrderAfternoontime(0);
        }
        return orderMapper.updateOrder(order);
    }

    @Override
    public int delete(Long[] ids) {
        return orderMapper.delete(ids);
    }

    @Override
    public List<Order> export(Order order,int orderState) {
        return orderMapper.selectOrderList(order,orderState);
    }

    
}
